package com.example.studentsystem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.studentsystem.bean.UserBean;
import com.example.studentsystem.dao.MyDao;

import java.util.List;

public class Adapter extends BaseAdapter {
    ListView lv;
    Context context;
    List<UserBean> student;


    public Adapter(Context context, List<UserBean> student,ListView lv) {
        this.context = context;
        this.student = student;
        this.lv = lv;

    }


    @Override
    public int getCount() {
        return student.size();
    }

    @Override
    public Object getItem(int i) {
        return student.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view == null)
             view = View.inflate(context,R.layout.activity_adapter,null);
         TextView id = view.findViewById(R.id.id);
         TextView name = view.findViewById(R.id.name);
        TextView sex = view.findViewById(R.id.sex);
        TextView banji = view.findViewById(R.id.banji);
        Button update = view.findViewById(R.id.update);
        Button delete = view.findViewById(R.id.delete);
        id.setText(student.get(i).getId()+"");
        name.setText(student.get(i).getName());
        sex.setText(student.get(i).getSex());
        banji.setText(student.get(i).getBanji());
        final MyDao myDao = new MyDao(context);
        final UserBean userBean = new UserBean();
        userBean.setId(student.get(i).getId());
        userBean.setName(student.get(i).getName());
        userBean.setSex(student.get(i).getSex());
        userBean.setBanji(student.get(i).getBanji());
        userBean.setNum(student.get(i).getNum());
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                student.remove(i);
                myDao.delete(userBean);
                lv.setAdapter(new Adapter(context,student,lv));
            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
               intent.setClass(context,UPDATE.class);
               intent.putExtra("userbean",userBean);
                context.startActivity(intent);
                //lv.setAdapter(new Adapter(context,student,lv));
            }
        });
        return view;
    }
}